<include file="public/header" />
<include file="public/left" />
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title"><a class="back" href="javascript:history.back();" title="返回列表">返回上一页</a>
            <div class="subject">
                <h3>信息编辑</h3>
                <h5>网站系统权限资源管理</h5>
            </div>
        </div>
    </div>
<form method="post" enctype="multipart/form-data">
    <section>
        <ul class="ulColumn2">
            <li>
                <span class="item_name" style="width:120px;">商品主图：</span>
                <input type="file" name="image" class="textbox textbox_295" />
            </li>
            <li>
                <span class="item_name" style="width:120px;">商品详情图：</span>
                <input type="button" value="添加" onclick="addImg(this)" />
            </li>
            <li>
                <span class="item_name" style="width:120px;">商品名称：</span>
                <input type="text" name="name" class="textbox textbox_295" />
            </li>
            <li>
                <span class="item_name" style="width:120px;">商品描述：</span>
                <input type="text" name="des" class="textbox textbox_295" />
            </li>
            <li>
                <span class="item_name" style="width:120px;">商品保质期：</span>
                <input type="text" name="keepday" class="textbox textbox_295" />(7天)
            </li>
            <li>
                <span class="item_name" style="width:120px;">商品标签：</span>
                <label><input type="radio" name="label" value="0" checked>无</label>
                <label><input type="radio" name="label" value="1" />招牌</label>
                <label><input type="radio" name="label" value="2" />新品</label>
                <label><input type="radio" name="label" value="3" />店长推荐</label>
            </li>
            <li>
                <span class="item_name" style="width:120px;">店内分类：</span>
                <select  name="cid" class="textbox textbox_295 selecto" />
                    <volist name="category" id="vo">
                        <option value="{$vo['id']}">{$vo['name']}</option>
                    </volist>
                </select>
            </li>
            <li>
                <span class="item_name" style="width:120px;">价格：</span>
                <input type="text" name="price" class="textbox textbox_295" />
            </li>
            <li>
                <span class="item_name" style="width:120px;">原价：</span>
                <input type="text" name="oldprice" class="textbox textbox_295" />
            </li>
            <li>
                <span class="item_name" style="width:120px;">库存：</span>
                <input type="text" name="stock" class="textbox textbox_295" />
            </li>
            <li>
                <span class="item_name" style="width:120px;">商品单位：</span>
                <input type="text" name="unit" class="textbox textbox_295" />
            </li>
            <li>
                <span class="item_name" style="width:120px;">状态：</span>
                <input type="radio" name="status" value="1" checked>上架
                <input type="radio" name="status" value="0" >下架
            </li>
            <style>
                .table{width: 300px;margin-left: 120px;text-align: center}
            </style>
            <li>
                <span class="item_name" style="width:120px;">规格： </span><input class="link_btn" type="button" onclick="addOption()" value="增加规格" />
            </li>
            <li>
                <table class="table" id="option">
                    <tr><th>操作</th><th>规格名称</th><th>价格</th><th>库存</th></tr>
               <!--     <tr>
                        <td >
                            <div style="width: 50px;">
                                <a href="javascript:;" onclick="deleteParam1(this)"  style="margin-top:10px;" title="删除">删除</a>
                            </div>
                        </td>
                        <td><input type="text" name="spec_name[]" /></td>
                        <td><input type="text" name="spec_price[]" /></td>
                        <td><input type="text" name="spec_stock[]"  /></td>
                    </tr>-->
                </table>
            </li>
            <li>
                <span class="item_name" style="width:120px;">属性： </span><input class="link_btn" type="button" onclick="addParam()" value="增加属性" /><span style="color:red;margin-left: 10px">* 多个属性值时，用英文逗号隔开</span>
            </li>
            <li>
                <table class="table" id="param">
                    <tr><th>操作</th><th>属性名称</th><th>属性值</th></tr>
                  <!--  <tr>
                        <td >
                            <div style="width: 50px;">
                            <a href="javascript:;" onclick="deleteParam1(this)"  style="margin-top:10px;" title="删除">删除</a>
                            </div>
                        </td>
                        <td><input type="text" name="param_name[]" /></td>
                        <td><input type="text" name="param_option[]"  style="width: 270px"/></td>
                    </tr>-->
                </table>
            </li>
            <li>
                <span class="item_name" style="width:120px;"></span>
                <input type="button" class="link_btn1 link_btn" value="提交" />
            </li>
        </ul>
    </section>
</form>
    <script type="text/javascript" src="__STATIC__/admin/js/jquery-ui-1.10.3.min.js"></script>
    <script>
        function addImg(obj) {
            var li = $(obj).parent();
            var html = '<span class="addimg">'
                    +'<input type="file" name="imgs[]" value="">'
                    +'<span onclick="deleteImg(this);" style="color: #dd0000;">删除</span>'
                    +'</span>';
            li.append(html);
            return;
        }
        function deleteImg(obj) {
            $(obj).parent('.addimg').remove();
            return;
        }
        function addOption() {
            $.ajax({
                "url": "{:url('goods/optiontpl')}",
                success: function(data) {
                    $('#option').append(data);
                }
            });
            return;
        }
        function addParam() {
            $.ajax({
                "url": "{:url('goods/paramtpl')}",
                success: function(data) {
                    $('#param').append(data);
                }
            });
            return;
        }
        function deleteParam1(o) {
            $(o).parents("tr").remove();

            return;

        }
		
		
        $(".link_btn1").click(function(){
            var formData = new FormData();
            var name = $('input[name="name"]').val();
            var des = $('input[name="des"]').val();
            var keepday = $('input[name="keepday"]').val();
            var label = $('input[name="label"]:checked').val();
			var cid = $('.selecto option:selected').val();
			var price = $('input[name="price"]').val();
			var oldprice = $('input[name="oldprice"]').val();
			var stock = $('input[name="stock"]').val();
			var unit = $('input[name="unit"]').val();
			var file = $(':file')[0].files[0];
//console.log(file);
            for(var i=1;i<$(':file').length;i++)
            {
                var f = $(':file')[i].files[0];
                formData.append('images[]',f);
            }
			var specs1 = [];
			var param1 = [];
			 
			for(var i = 1; i<$('#option tr').length; i++){
				var optioni = $('#option tr')[i];
				var spec_name = optioni.children[1].children[0].value;
				var spec_price = optioni.children[2].children[0].value;
				var spec_stock = optioni.children[3].children[0].value 
			    var spec = {"title": spec_name, "price": spec_price, "stock": spec_stock, "spec_id":0};
			    specs1.push(spec);
            }
            for(var i = 1; i< $('#param tr').length; i++){
				var parami =$('#param tr')[i];				
				var param_name = parami.children[1].children[0].value;
				var param_option = parami.children[2].children[0].value;
				var para = { "title": param_name, "param": param_option, "param_id":1};
				param1.push(para);
            }
			var param = JSON.stringify(param1);
			var specs = JSON.stringify(specs1)

			formData.append('name',name);
			formData.append('des',des);
			formData.append('keepday',keepday);
			formData.append('label',label);
			formData.append('cid',cid);
			formData.append('price',price);
			formData.append('oldprice',oldprice);
			formData.append('stock',stock);
			formData.append('unit',unit);
			formData.append('image',file);
			formData.append('params',param);
			formData.append('specs',specs);
			formData.append('images',files);

			$.ajax({
                //url:"https://tjdw.gzldrj.com:441/api/system/goods_edit",
				url:"{:url('goods/goodsadd')}",
                type:'POST',
                data:formData,
                async:false,
                processData:false,
                contentType:false,
                success:function(result){
//                    console.log(result.code)
//                    if(result.code == 0){
//                        alert(result.msg);
//                    }else{
//                        location.href = "{:url('goods/index')}";
//                    }
                }
            });
        })
    </script>
</div>
</section>
</body>

</html>